import { createAction } from 'redux-actions';
import * as types from '../constants/ActionTypes';
import * as Api from '../utils/WebAPIUtils.js';
import Tool from '../utils/ToolUtils.js';

const getTestCategoryAction = createAction(types.FETCH_TEST_CATEGORY, data => data);
const getpushCategoryLogAction = createAction(types.FETCH_PUSH_CATEGORY_LOG, data => data);
const saveMsg = createAction(types.SAVE_TEST_CATEGORY_ERROR_MSG, data => data);

exports.fetchTestCategoryList = (data) => (dispath) =>
  Tool.post(Api.testCategory.query, data, (res) => {
    dispath(getTestCategoryAction(res));
  });

// 新增测试包信息
// ==================
exports.saveTestCategory = (data, router) => (dispath) =>
  Tool.post(Api.testCategory.new, data, (res) => {
    if (res.code === 200) {
      dispath(saveMsg(res));
      Tool.post(Api.testCategory.query, data, (resData) => {
        dispath(getTestCategoryAction(resData));
      });
    } else if (res.code === 90001) {
      console.log('asdfasd');
    }
  }, (error) => { console.log('error'); });

// 新增测试包信息
// ==================
exports.editTestCategory = (data, router) => (dispath) =>
  Tool.post(Api.testCategory.edit, data, (res) => {
    if (res.code === 200) {
      dispath(saveMsg(res));
      Tool.post(Api.testCategory.query, data, (resData) => {
        dispath(getTestCategoryAction(resData));
      });
    } else if (res.code === 90001) {
      console.log('asdfasd');
    }
  }, (error) => { console.log('error'); });


exports.fetchPushCategoryLog = (data) => (dispath) =>
  Tool.post(Api.pushCategoryLog.query, data, (res) => {
    dispath(getpushCategoryLogAction(res));
  });
